package com.hdoit.ares.web.weixin;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

import org.junit.Test;

import com.tennetcn.common.selfcommon.message.wechat.Button;
import com.tennetcn.common.selfcommon.message.wechat.ComplexButton;
import com.tennetcn.common.selfcommon.message.wechat.Menu;
import com.tennetcn.common.selfcommon.message.wechat.ViewButton;
import com.tennetcn.common.utils.JsonUtils;


public class WxGetMenu {
	//@Test
	public void createMenu(){
		WxMpService wxService = new WxMpServiceImpl(); 
		wxService.setWxMpConfigStorage(getConfig());
		
		String appId=wxService.getWxMpConfigStorage().getAppId();
		String menuJson=getMenu(appId);	
		
		System.out.println(menuJson);
		
		
			String menuId;
			try {
				menuId = wxService.getMenuService().menuCreate(menuJson);
				System.out.println(menuId);
			} catch (WxErrorException e) {
				e.printStackTrace();
			}
		
		
	}
	
	private String getMenu(String appId){

//		公司的服务器
		String domain="http://www.putiyoli.net.cn";

		
		ViewButton exclusiveService = new ViewButton();
		exclusiveService.setName("专属服务");
		exclusiveService.setType("view");
		exclusiveService.setUrl(domain+"/ares/we/index");
		

		ViewButton myCodeButton = new ViewButton();
		myCodeButton.setName("我的二维码");
		String discountViewUrl=domain+"/ares/we/shareCode";
		myCodeButton.setType("view");
		myCodeButton.setUrl(discountViewUrl);

		
		
		ComplexButton imSharedButton = new ComplexButton();
		imSharedButton.setName("我要分享");
		imSharedButton.setSub_button(new Button[] {myCodeButton});
		
		
		ViewButton personCenter = new ViewButton();
		personCenter.setName("个人中心");
		String personCenterViewUrl=domain+"/ares/we/personCenter";
		personCenter.setType("view");
		personCenter.setUrl(personCenterViewUrl);
		
		ComplexButton myButton = new ComplexButton();
		myButton.setName("我的");
		myButton.setSub_button(new Button[] {personCenter});
		
	
		Menu menu = new Menu();
		menu.setButton(new Button[] { exclusiveService,imSharedButton, myButton });
		
		
		
		return JsonUtils.toJson(menu);
	}
	
	private WxMpInMemoryConfigStorage getConfig(){
		WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
	
		//普天永利
		config.setAppId("wx62ca56fd226591a6"); // 设置微信公众号的appid
		config.setSecret("b33f4139237da703c218a36f4e74a348"); // 设置微信公众号的app corpSecret

		config.setToken(""); // 设置微信公众号的token
		config.setAesKey(""); // 设置微信公众号的EncodingAESKey
		
		return config;
	}
}
